﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ReflectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            fun1();
            fun2();
            fun3();
        }
        //获取类型信息
        static void fun1()
        {
            Type type = typeof(MyClass);
            Console.WriteLine("type.Name:" + type.Name);
            Console.WriteLine("type.FullName:" + type.FullName);
            Console.WriteLine("Namespace:" + type.Namespace);
            Console.WriteLine("Assembly:" + type.Assembly);
            Console.WriteLine("Module:" + type.Module);
            Console.WriteLine("BaseType:"+type.BaseType);
            Console.WriteLine("IsClass:"+type.IsClass);
            MemberInfo[] memberInfos = type.GetMembers();
            foreach(var item in memberInfos)
            {
                Console.WriteLine(string.Format("Members: {0}:{1}", item.MemberType, item));
            }
        }
        //获取程序集信息
        static void fun2()
        {
            Assembly assembly = Assembly.LoadFrom("./ReflectionDemo.exe");
            Console.WriteLine("程序集全名："+assembly.FullName);
            Console.WriteLine("程序集的版本："+assembly.GetName().Version);
            Console.WriteLine("程序集初始位置："+assembly.CodeBase);
            Console.WriteLine("程序集入口："+assembly.EntryPoint);
            Type[] types = assembly.GetTypes();
            foreach(var item in types)
            {
                Console.WriteLine("类："+item.Name);
            }
        }
        //反射调用方法
        static void fun3()
        {
            Assembly assembly = Assembly.LoadFrom("./ReflectionDemo.exe");
            Type type = assembly.GetType("ReflectionDemo.MyClass");
            object obj = Activator.CreateInstance(type);
            MethodInfo mi = type.GetMethod("test");
            mi.Invoke(obj,null);
        }
    }

    class MyClass
    {
        public string m;
        public void test()
        {
            Console.WriteLine("test() run....");
        }
        public int MyProperty { get; set; }
    }
}
